define("zui/pagestore",["zui/utils","zui/dialog"],function(require,exports,module){

	 zDialog=require("zui/dialog");
	 zUtil=require("zui/utils");

	 var resultMap={
			limit:"limit",
			pageSize:"pageSize",
			pageIndex:"pageIndex",
			start:"start",
			totalCount:"totalCount",
			totalPageIndex:"totalPageIndex",
			hasErrorProperty:"hasErrorCode",
			result:"rows"
	};


	 function zStore(options,callback){
	  			this.defaultOptions={
	  						autoLoad:true,
	  						srcNode:"",
				  			url:null,
				  			localData:{},
				  			data:{},
							ajaxoptions:{
				  				method:"GET",
				  				dataType:"json"
				  			},
				  			itemRenderTpl:null,
				  			handleListener:{
				  				
				  				itemclick:null

				  			}
				  			
		  			};

	   			this._init(options,callback);
	   }

	   zStore.prototype={

	   			_init:function(options,callback){

	   					var _self=this;
	   					if(!options.srcNode||options.srcNode=="") return;
	   					_self.el=$(options.srcNode);
	   					_self.defaultOptions=$.extend(_self.defaultOptions,options||{});
	   					_self._resultDataMap();
	   					if(!_self.defaultOptions['url']&&_self.defaultOptions['localData']){
	   						 _self._localData(callback);
						}
						else{
		   					if(_self.defaultOptions['autoLoad']){
		   								_self._ajaxproxy(callback);
		   					}
	   					}
	   							


	   			},
	   			getResult:function(){
	   					var _self=this;
	   					return _self._getResult();
	   			},
	   			load:function(params,callback){
	   				 var _self=this;
	   				 var pageattrs=_self.defaultOptions['data'];
	   				 var data=$.extend(pageattrs,params);
	   				 _self.defaultOptions=$.extend(_self.defaultOptions,data||{});
	   				 _self._ajaxproxy(callback);
	   			},
	   			_ajaxproxy:function(callback){
	   					var _self=this;
	   					var cfg=_self._getAjaxOptions(),
	   					error=cfg.error,
	   					success=cfg.success;
	   					zDialog.Loading();
	   		  			cfg.success=function(data){
	   		  					_self._setResult(data);

	   		  					if(zUtil.isFunction(callback)){
	   		  						  callback(data);
	   		  					}
	   		  					 zDialog.closeDialog();

	   		  			};
	   		  			cfg.error=function(jqXHR, textStatus, errorThrown){
					        //error && error(jqXHR, textStatus, errorThrown);
					        zDialog.closeDialog();
					        var result = {
					            exception : {
					              status : textStatus,
					              errorThrown: errorThrown,
					              jqXHR : jqXHR
					            }
					          };
					         if(zUtil.isFunction(callback)){
	   		  						  callback(result);
	   		  				 }
					      };

	   					$.ajax(cfg);


	 			 },
	 			 _localData:function(callback){
	 			 			var _self=this;
	 			 			_self._setResult(_self.defaultOptions['localData']);

   		  					if(zUtil.isFunction(callback)){
   		  						  callback(_self.defaultOptions['localData']);
   		  					}

	 			 },
	 			 _resultDataMap:function(){
	 			 	var _self=this;
	 			 	if(_self.defaultOptions['resultMap']){
	 			 		_self._resultMap=zUtil.dataMapResult(resultMap,_self.defaultOptions['resultMap']);
	 			 	}
	 			 	else{
	 			 		_self._resultMap=resultMap;
	 			 	}
	 			 },
	 			 _getAjaxOptions:function(){
	 			 	var _self=this,
	 			 	ajaxoptions=_self.defaultOptions['ajaxoptions'],
	 			 	url=_self.defaultOptions['url'],
	 			 	params=_self.defaultOptions['data'];
	 			 	
	 			 	return $.extend({},ajaxoptions,{url:url,data:params});


	 			 },
	 			 _setResult:function(data){
	 			 	var _self=this;
	 			 	if(resultMap['result'].split(".").length>1){
	 			 		var rowMap= resultMap['result'].split(".");
	 			 		var  rows=data[rowMap[0]];
	 			 		for(var i=1;i<rowMap.length;i++){
	 			 			rows=rows[rowMap[i]];
	 			 		}
	 			 	}
	 			 	else{
	 			 		var rows=data[resultMap['result']];
	 			 	}
	 			 	_self.records=rows;
	 			 	_self.result=data;
	 			 	_self._setTemplate();

	 			 },
	 			 _getResult:function(){
	 			 	var _self=this;
	 			 	return _self.records;

	 			 },
	 			 _setTemplate:function(){
	 			 	var _self=this;
	 			 	var str="";
	 			 	var _selector="";
	 			 	var rows=_self.records;
	 			 	
	 			 	if(rows||rows.length>0){
	 			 		//var data={"rows":rows};
	 			 		for(var i=0;i<rows.length;i++){
	 			 			if(zUtil.isFunction(_self.defaultOptions['itemRenderTpl']))
		 			 			str+=_self.defaultOptions['itemRenderTpl'](rows[i]);
		 			 		else
		 			 			str+=zUtil.substitute(_self.defaultOptions['itemTpl'],rows[i]);
		 			 		
	 			 		}
	 			 		_self.el.html(str);
	 			 		_self._setItemData();
	 			 	}
	 			 

	 			 },
	 			 _setItemData:function(){
	 			 		var _self=this;
	 			 		var itemList=$(_self.el).children();
	 			 		_self.listEl=itemList;
	 			 		var  records=_self._getResult();
	 			 		zUtil.each(itemList,function(elem,index){
	 			 				$(elem).data("record",records[index]);
	 			 		});

	 			 		_self._handleEvent();

	 			 },
	 			 _handleEvent:function(){
		 			 	var _self=this;
		 			 	var itemList=_self.listEl;
		 			 	if(zUtil.isFunction(_self.defaultOptions.handleListener['itemclick'])){
			 			    zUtil.each(itemList,function(elem,index){

			 			    		$(elem).on("click",function(e){
				 			 			var _item=$(this).data("record");
				 			 			var _domTarget=e.target;
				 			 			
 			 							_self.defaultOptions.handleListener['itemclick']({
				 			 				record:_item,
				 			 				domTarget:_domTarget
				 			 			});
			 							
			 			 		
		 							});
		 			 		});

	 			 		 }


	 			    }
	 			 	


	   }




	   	return zStore;

});